<?php


namespace app\common\controller;

use app\common\model\Picture;
use think\Request;

/**
 * 工具类
 * Class Tools
 * @package app\common\controller
 */
class Tools
{
    private static $host = '';

    public function __construct()
    {
        //TODO 不是很建议后端配固定的图片host 原因（当前业务可以不考虑）：如果图片过大专门配置图片服务器或者要增加CDN 则之前上传的图片由于加了host 会导致新的图片会被优化 但是以前的图片不会被做任何优化处理
        self::$host = Request::instance()->domain() . '/';
    }

    /**
     * 上传图片
     * @param int $type
     * @param $file
     */
    public function uploadImageFile($type, $file)
    {
        $info = $file->validate(['ext' => 'jpg,jpeg,png,gif,webp'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'images');
        if (!$info) {
            //上传失败输出错误信息
            return [
                'success' => false,
                'msg' => 'illegal image files!'
            ];

        }
        // 成功上传后 获取上传信息
        $url = 'uploads' . DS . 'images' . DS . $info->getSaveName();
        $url = str_replace(DS, '/', $url);
        //处理图片生成压缩图
//        $name = explode('.', $info->getSaveName());
//        $image = \think\Image::open(ROOT_PATH . 'public' . DS . $url);
//        $thumbUrl = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'images' . DS . $name[0] . '_thumb.jpg';
//        $image->thumb(100, 100)->save($thumbUrl);
//        $thumb = 'uploads' . DS . 'images' . DS . $name[0] . '_thumb.jpg';
//        $thumb = str_replace(DS, '/', $thumb);
        $setData['url'] = self::$host . $url;
//        $setData['thumb_url'] = self::$host . $thumb;
        $setData['type'] = $type;
        $setData['create_time'] = time();
        $picture = new Picture;
        $id = $picture->insertGetId($setData);
        if ($id) {
            unset($setData['create_time']);
            $data = $setData;
            $data['id'] = $id;
            $ret['success'] = true;
            return [
                'success' => true,
                'msg' => '',
                'data' => $data
            ];
        }
        return [
            'success' => false,
            'msg' => '上传文件失败,请重试'
        ];
    }

    /**
     * 上传视频
     * @param $type
     * @param $file
     */
    public function uploadVideoFile($type, $file)
    {
        $info = $file->validate(['ext' => 'mp4,wmv,flv,avi'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'video');
        if (!$info) {
            //上传失败输出错误信息
            return [
                'success' => false,
                'msg' => $file->getError()
            ];
        }
        // 成功上传后 获取上传信息
        $url = 'uploads' . DS . 'video' . DS . $info->getSaveName();
        $url = str_replace(DS, '/', $url);
        $setData['url'] = self::$host . $url;
        $setData['type'] = $type;
        $setData['create_time'] = time();
        $picture = new Picture;
        $id = $picture->insertGetId($setData);
        if ($id) {
            unset($setData['create_time']);
            $data = $setData;
            $data['id'] = $id;
            $ret['success'] = true;
            return [
                'success' => true,
                'msg' => '',
                'data' => $data
            ];
        }
        return [
            'success' => false,
            'msg' => '上传文件失败,请重试'
        ];

    }


}